﻿using log4net;
using NCP.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NCP.Core.Exceptions
{
    public class MessageException : Exception
    {

        public MessageException() { }
        public MessageException(string message)
            : base(message)
        {
        }
        public MessageException(Exception innerException)
            : base(innerException.Message, innerException)
        {
        }
        public MessageException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
        public MessageException(string message, object tag)
            : base(message)
        {
            Tag = tag;
        }
        public MessageException(string message, int code, object tag)
            : base(message)
        {
            Code = code;
            Tag = tag;
        }
        public MessageException(string message, int code, Exception innerException, object tag)
            : base(message, innerException)
        {
            Code = code;
            Tag = tag;
        }

        public int Code { get; set; } = 2;

        public object Tag { get; set; }

        public static void CheckNullException(object obj, string message, object tag = null)
        {
            if (obj is string str)
            {
                CheckNullException(str, message, tag);
            }
            else if (obj == null)
            {
                throw new MessageException(message, tag);
            }
        }


        public static void CheckNullException(string str, string message, object tag = null)
        {
            if (string.IsNullOrEmpty(str))
            {
                throw new MessageException(message, tag);
            }
        }

        public static void CheckException(bool arg, string message, object tag = null)
        {
            if (arg)
            {
                throw new MessageException(message, tag);
            }
        }
        public static void CheckNullException(object obj, int code, string message, object tag = null)
        {
            if (obj is string str)
            {
                CheckNullException(obj, code, message, tag = null);
            }
            else if (obj == null)
            {
                throw new MessageException(message, code, tag);
            }
        }


        public static void CheckNullException(string str, int code, string message, object tag = null)
        {
            if (string.IsNullOrEmpty(str))
            {
                throw new MessageException(message, code, tag);
            }
        }

        public static void CheckException(bool arg, int code, string message, object tag = null)
        {
            if (arg)
            {
                throw new MessageException(message, code, tag);
            }
        }

        public static void CatchException(MessageException ex, LayData data)
        {
            data.code = ex.Code != 0 ? ex.Code.ToString() : "2";
            data.msg = ex.Message;
        }
        public static void CatchException(Exception ex, LayData data)
        {
            if (ex is MessageException)
            {
                CatchException((MessageException)ex, data);
            }
            else
            {
                ILog logger = LogManager.GetLogger(typeof(Exception));
                logger.Error(ex.Message, ex);
                data.code = "-1";
                data.msg = $"异常错误信息! 错误代码：{ex.Message}";
            }
        }
    }
}
